﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConjureBivouac2012.GameStates;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace ConjureBivouac2012.Object
{
    public class MonsterB : AbstractEnemy
    {
        bool newBulletNeeded = false;
        private float fireDelay;
        private double lastShootFired;

        public MonsterB(float fireDelay, double lastShootFired) : base(80, 5)
        {
            this.fireDelay = fireDelay;
            this.lastShootFired = lastShootFired;
        }

        // Return a new bullet that has been shoteed
        public override Entity[] addingEntity()
        {
            SpecialBullet[] bullets = new SpecialBullet[4];
            if (newBulletNeeded)
            {
                newBulletNeeded = false;

                for (int i = 0; i < 4; i++)
                {
                    SpecialBullet b = new SpecialBullet();
                    bullets[i] = b;
                }
                bullets[0].Fire(this, new Vector2(-1,-1));
                bullets[1].Fire(this, new Vector2(-1, 1));
                bullets[2].Fire(this, new Vector2( 1,-1));
                bullets[3].Fire(this, new Vector2( 1, 1));
                return bullets;
            }
            else return null;
        }

        public override void LoadContent(ContentManager contentManager)
        {
            contentName = "Ennemy/EnnemyB";
            this.sprite.LoadContent(contentManager, contentName);
            base.LoadContent(contentManager);
        }

        public override void Update(GameTime time)
        {
            base.Update(time);
            Fire(time);
        }

        public void Fire(GameTime time)
        {
            if (fireDelay > 0 && (lastShootFired == -1 || time.TotalGameTime.TotalSeconds - lastShootFired >= fireDelay))
            {
                lastShootFired = time.TotalGameTime.TotalSeconds;
                newBulletNeeded = true;
            }
        }

         private Vector2 GetFireDirection()
         {
             Vector2 direction = new Vector2(0, 0);

             IGlobalGameState state = Program.GetGameInstance.GetCurrentState();

             AbstractPlayer p = null;

             if(state is PlayGame)
             {
                 p = ((PlayGame) state).GetPlayer();
             }

             if (p != null)
             {
                 direction = p.GetVehicule().sprite.Position - this.sprite.Position;
                 direction.Normalize();
             }

             return direction;
         }
    }
}
